// pages/dd_range/dd_range.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getUserRange, setUserRange } from '../../static/api/api.js'
let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        rangeData:null,
        distanceSelectedId:'',
        areaSelectRange:[],
        distanceRange:[
            {
                name:'全岛内',
                id:'-1'
            },
            {
                name: '1KM',
                id: '1'
            },
            {
                name: '2KM',
                id: '2'
            },
            {
                name: '3KM',
                id: '3'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserRange()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //获取用户服务
    getUserRange() {
        let that = this;
        wx.showLoading({
            title: '加载中...',
        })
        getUserRange().then(response => {
            wx.hideLoading()
            console.log(response)
            let sArr = response.data.store_user.service_area_ids?response.data.store_user.service_area_ids.split(','):[];
            sArr.forEach((el, index) => {
                response.data.service_area.forEach((cel, cindex) => {
                    if (el == cel.id) {
                        cel.isSelected = true;
                    }
                })
            })
            that.setData({
                'rangeData': response.data,
                'distanceSelectedId': response.data.store_user.service_distance
            })
            
        }).catch(response => {
            wx.hideLoading()
        })

    },
    //设置用户服务范围
    setUserRange() {
        let that = this;
        let cArr = [];
        this.data.rangeData.service_area.forEach((el,index)=>{
            if(el.isSelected){
                cArr.push(el.id);
            }
        })
        let params={
            service_distance: this.data.distanceSelectedId,
            service_area_ids: cArr.join(',')
        }
        wx.showLoading({
            title: '加载中...',
        })
        setUserRange(params).then(response => {
            wx.hideLoading()
            console.log(response)
            wx.showToast({
                icon:'',
                title: '操作成功',
            })
            setTimeout((function(){
                wx.navigateBack({
                    
                })
            }),1000)
        }).catch(response => {
            wx.hideLoading()
        })

    },
    areaTypeChange(e){
        let dataset = e.currentTarget.dataset;
        this.setData({
            distanceSelectedId:'',
            ['rangeData.service_area[' + dataset.index + '].isSelected']: !this.data.rangeData.service_area[dataset.index].isSelected
        })
    },
    distanceTypeChange(e){
        let dataset = e.currentTarget.dataset;
        this.setData({
            distanceSelectedId: dataset.id
        })
        this.data.rangeData.service_area.forEach((el,index)=>{
            this.setData({
                ['rangeData.service_area[' + index + '].isSelected']:false
            })
        })
    }
})